/**
 * 
 */
package co.edu.eam.iso.excel;

import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

import co.edu.eam.iso.enums.CharacteristicEnum;
import co.edu.eam.iso.enums.SubCharacteristicEnum;
import co.edu.eam.iso.evaluated.CharacteristicEvaluated;
import co.edu.eam.iso.evaluated.MetricEvaluated;
import co.edu.eam.iso.evaluated.ProductEvaluated;
import co.edu.eam.iso.evaluated.SubCharacteristicEvaluated;

/**
 * @author Giovanny Ramirez Blandon
 * @version 1.0
 */
public class ExcelGenerator {
	
	private static ExcelGenerator instance = null;

	public HSSFWorkbook generateExcel(ProductEvaluated productEvaluated) {
		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet sheet = book.createSheet();
		int rows = 0;

		HSSFRow productRow = sheet.createRow(rows);
		rows++;
		
		Cell productCell = productRow.createCell(0);
		productCell.setCellValue("Calidad producto");
		sheet.autoSizeColumn(0);
		
		Cell qaProductCell = productRow.createCell(1);
		qaProductCell.setCellValue(productEvaluated.getQuality().toString());
		sheet.autoSizeColumn(1);
		
		for (Entry<CharacteristicEnum, CharacteristicEvaluated> chrctcEvaluated : productEvaluated
				.getCharacteristics().entrySet()) {
			HSSFRow chrctcRow = sheet.createRow(rows);
			rows++;			

			Cell chrctcLblCell = chrctcRow.createCell(1);
			chrctcLblCell.setCellValue("Característica");
			sheet.autoSizeColumn(1);
			
			Cell chrctcCell = chrctcRow.createCell(2);
			chrctcCell.setCellValue(chrctcEvaluated.getKey().toString());
			sheet.autoSizeColumn(2);
			
			HSSFRow chrctcqaRow = sheet.createRow(rows);
			rows++;			
			
			Cell qaChrctcLblCell = chrctcqaRow.createCell(1);
			qaChrctcLblCell.setCellValue("Calidad característica");
			sheet.autoSizeColumn(1);			
			
			Cell qaChrctcCell = chrctcqaRow.createCell(2);
			qaChrctcCell.setCellValue(chrctcEvaluated.getValue().getQuality().toString());
			sheet.autoSizeColumn(2);
			
			HSSFRow chrctcWeightRow = sheet.createRow(rows);
			rows++;			

			Cell weightChrctcLblCell = chrctcWeightRow.createCell(1);
			weightChrctcLblCell.setCellValue("Valor característica");
			sheet.autoSizeColumn(1);			
			
			Cell weightChrctcCell = chrctcWeightRow.createCell(2);
			weightChrctcCell.setCellValue(chrctcEvaluated.getValue().getCharacteristic().getWeight().toString());
			sheet.autoSizeColumn(2);	
			
			HSSFRow qaVSTotalChrctcRow = sheet.createRow(rows);
			rows++;			

			Cell qaVSTotalChrctcLblCell = qaVSTotalChrctcRow.createCell(1);
			qaVSTotalChrctcLblCell.setCellValue("Calidad VS Total");
			sheet.autoSizeColumn(1);			
			
			Cell qaVSTotalChrctcCell = qaVSTotalChrctcRow.createCell(2);
			qaVSTotalChrctcCell.setCellValue(chrctcEvaluated.getValue().getQualityVStotal().toString());
			sheet.autoSizeColumn(2);			
			
			for (Entry<SubCharacteristicEnum, SubCharacteristicEvaluated> subChrctcEvaluated : chrctcEvaluated
					.getValue().getSubCharacteristics().entrySet()) {
				HSSFRow subChrctcRow = sheet.createRow(rows);
				rows++;			

				Cell subChrctcLblCell = subChrctcRow.createCell(2);
				subChrctcLblCell.setCellValue("Subcaracterística");
				sheet.autoSizeColumn(2);
				
				Cell subChrctcCell = subChrctcRow.createCell(3);
				subChrctcCell.setCellValue(subChrctcEvaluated.getKey().toString());
				sheet.autoSizeColumn(3);

				HSSFRow subChrctcqaRow = sheet.createRow(rows);
				rows++;			
				
				Cell subQaChrctcLblCell = subChrctcqaRow.createCell(2);
				subQaChrctcLblCell.setCellValue("Calidad subcaracteristíca");
				sheet.autoSizeColumn(2);			
				
				Cell subQaChrctcCell = subChrctcqaRow.createCell(3);
				subQaChrctcCell.setCellValue(subChrctcEvaluated.getValue().getQuality().toString());
				sheet.autoSizeColumn(3);
				
				HSSFRow subChrctcWeightRow = sheet.createRow(rows);
				rows++;		
				
				Cell subWeightChrctcLblCell = subChrctcWeightRow.createCell(2);
				subWeightChrctcLblCell.setCellValue("Valor subcaracteristíca");
				sheet.autoSizeColumn(2);			
				
				Cell subWeightChrctcCell = subChrctcWeightRow.createCell(3);
				subWeightChrctcCell.setCellValue(subChrctcEvaluated.getValue().getSubCharacteristic().getWeight().toString());
				sheet.autoSizeColumn(3);				
				
				for (MetricEvaluated metric : subChrctcEvaluated.getValue()
						.getMetrics()) {					
					HSSFRow metricLblRow = sheet.createRow(rows);
					rows++;			

					Cell metricLblCell = metricLblRow.createCell(3);
					metricLblCell.setCellValue("Métrica");
					sheet.autoSizeColumn(3);
					
					Cell metricCell = metricLblRow.createCell(4);
					metricCell.setCellValue(metric.getMetric().getDescription());
					sheet.autoSizeColumn(4);

					HSSFRow metricEvalRow = sheet.createRow(rows);
					rows++;			
					
					Cell metricEvalLblCell = metricEvalRow.createCell(3);
					metricEvalLblCell.setCellValue("Evaluación");
					sheet.autoSizeColumn(3);			
					
					Cell metricEvalCell = metricEvalRow.createCell(4);
					metricEvalCell.setCellValue(metric.getMetric().getScale().toString());
					sheet.autoSizeColumn(4);
					
					HSSFRow metricQaRow = sheet.createRow(rows);
					rows++;			
					
					Cell metricQaLblCell = metricQaRow.createCell(3);
					metricQaLblCell.setCellValue("Calidad métrica");
					sheet.autoSizeColumn(3);			
					
					Cell metricQaCell = metricQaRow.createCell(4);
					metricQaCell.setCellValue(metric.getQuality().toString());
					sheet.autoSizeColumn(4);
				}
			}
		}
		
        return book;
	}

	public static ExcelGenerator getInstance() {
		if (instance == null) {
			instance = new ExcelGenerator();
		}
		return instance;
	}
	
}
